"use strict"

class BasePackage {
	constructor(size, packageType = 0) {
		this._size = size
		this._packageType = packageType
		this.limit = 30
		this._items = []
	}

	getSize() {
		//获取背包大小
		return this._size
	}

	setSize() {
		//设置背包大小
		this._size = size
	}


	getPackageType() {
		//获取包裹类型
		return this._packageType
	}

	setPackageType(packageType) {
		//设置包裹类型//
		this._packageType = packageType
	}

	getItemList() {
		//获取物品列表//
		return this._items
	}

	getItems() {
		//获取包裹中的物品//
		return this._items
	}

	setItems(items) {
		//设置包裹中的物品
		this._items = items
	}

	getOneRecordByPosition(position) {
		//获取指定位置的物品以及物品的位置信息
		//@param position: int 表示包裹中的位置
		//
		for (let item of this._items) {
			if (item['position'] == position)
				return item
		}
		return null
	}

	getItemByPosition(position) {
		//根据坐标得到物品
		//@param position: int 物品的位置
		//
		for (let item of this._items) {
			if item['position'] == position:
				return item['itemComponent']
		}
		return null
	}

	getPositionByItemId(itemId) {
		//根据物品的ID查找物品在包裹中的位置
		//@
		//param itemId: int 物品的id
		for (let item of this._items) {
			if (item['itemComponent'].baseInfo.id == itemId)
				return item['position']
		}
		return -1 //没有找到时返回-1
	}

	getItemInfoByItemid(itemid) {
		//根据物品id获得物品实例
		//@param itemid: int 物品id
		//
		va = this.getPositionByItemId(itemid)
		if (va != -1)
			return this.getItemByPosition(va)
		else
			return null
	}

	putItemByPosition(position, itemComponent) {
		//根据位置放置物品
		//@param position: int 物品的位置
		//@param itemComponent: Item Object 物品
		//
		for (let item of this._items) {
			if (position == item['position']) {
				item['itemComponent'] = itemComponent
				return
			}
		}
		this._items.push({
			'position': position,
			'itemComponent': itemComponent
		})
	}

	transpositionItems(position, toposition) {
		//交换物品的位置//
		tag = 0
		fromItem = this.getItemByPosition(position)
		toItem = this.getItemByPosition(toposition)
		for (let item of this._items) {
			if (position == item['position']) {
				item['itemComponent'] = toItem
				tag += 1
			} else if (toposition == item['position']) {
				item['itemComponent'] = fromItem
				tag += 1
			}
			if (tag == 2)
				break
		}
	}

	updateItemStack(position, stack) {
		//更新物品层叠数//
		for (let i in this._items) {
			if (position == this._items[i]['position']) {
				if (stack == 0) {
					this._items.splice(i)
					return
				}
				fromItem['itemComponent'].pack.updateStack(stack)
			}
		}
	}

	removeItemByPosition(position) {
		//移除包裹中的物品//
		for (let i in this._items) {
			if (position == this._items[i]['position'])
				this._items.splice(i)
		}
	}
	moveItemByPosition(fromPosition, toPosition) {
		//移动包裹中的物品//
		for (fromItem in this._items) {
			if (fromPosition == fromItem['position']) {
				fromItem['position'] = toPosition
				return true
			}
			return false
		}
	}
	findSparePositionForItem() {
		//寻找包裹栏中可以放置物品的位置//
		for (let position = 1; position < this._size; position++) {
			result = this.getItemByPosition(position)
			if (result === null)
				return position
			return -1
		}
	}

	isFull() {
		// 判断临时包裹是否已满//
		position = this.findSparePositionForItem
		if (position === -1) {
			return True
		}
		return False
	}

	canPutItem(item, count) {
		//该position上能否放置物品
		//param toPosition: 目标位置@ param formerPosition: 原来的位置
		let maxstack = item.baseInfo.getItemTemplateInfo().get('maxstack', 1)
		let requiredcnt = math.ceil(count / (maxstack * 1.0))
		let nowcnt = this.findSparePositionNum()
		if (nowcnt < requiredcnt) {
			return False
		}
		return True
	}

	canMergeItem(toPosition, formerPosition) {
		//能否合并物品
		//@ param toPosition: 目标位置
		//@ param formerPosition: 原来的位置
		dragItem = this.getItemByPosition(formerPosition)
		destItem = this.getItemByPosition(toPosition)
		if (not destItem)
			return True
		destItemTemplate = destItem.baseInfo.getItemTemplateInfo()
		dragItemTemplate = dragItem.baseInfo.getItemTemplateInfo()
		if (dragItem === destItem)
			return True
		if (destItemTemplate['stack'] == -1)
			return False
		if (destItemTemplate['id'] !== dragItemTemplate['id'])
			return False
		return True
	}

	MergeItems(fromposition, toposition) {
		//合并物品//
		state = 0
		if (fromposition == toposition)
			return 1 //# 位置一致， 不能合并
		fromItem = this.getItemByPosition(fromposition)
		toItem = this.getItemByPosition(toposition)
		if (!fromItem || !toItem)
			return 1 //# 有物品不存在不能合并
		maxstack = fromItem.baseInfo.getItemTemplateInfo().get('maxstack',
			1)
		fromstack = fromItem.pack.getStack()
		tostack = toItem.pack.getStack()
		if (fromstack == maxstack)
			return 0 //# 起始位置物品堆叠数达到上限， 不能进行合并
		if (fromItem.baseInfo.getItemTemplateId() != toItem.baseInfo.getItemTemplateId())
			return 1 //# 物品类型不同不能合并
		if (tostack == maxstack) {
			this.transpositionItems(fromposition, toposition) //# 置换
			return 0
		}
		if (tostack + fromstack < maxstack) {
			startstack = tostack + fromstack
			endstack = 0
		} else {
			startstack = maxstack
		}
		endstack = tostack + fromstack - maxstack
		fromItem.pack.updateStack(startstack)
		if (startstack != maxstack)
			state = 1
		if (endstack <= 0)
			this.deleteItemInPackage(toposition)
		else {
			toItem.pack.updateStack(tostack + fromstack - maxstack)
		}
		return state
	}

	MergePackage() {
		//合并包裹中能合并的物品//
		positionList = [item['position']
			for item in this._items
		]
		for i in range(len(positionList)) {
			fromposition = positionList[i]
			if i == len(positionList) - 1:
				return
			for toposition in positionList[i + 1: ]:
				state = this.MergeItems(fromposition, toposition)
			if not state:
				break
		}
	}
}



clearPackage() {
		//清空包裹中的物品//
		this.setItems([])

		getPackageItemInfo() {
			//获取包裹所有物品信息//
			ItemsInfo = []
			for _item in this._items:
				itemInfo = {}
			itemInfo['position'] = _item['position']
			itemInfo['itemInfo'] = _item['itemComponent'].formatItemInfo()
			ItemsInfo.append(itemInfo)
			return ItemsInfo
		}
		dropItemByPosition(position) {
			//删除物品//
			for item in this._items:
				if item['position'] == position:
				this._items.remove(item)
		}
		putItemInstanceInPackDB(item, position, characterId) {
			//放置一个物品的实例到包裹中//
			this.putItemByPosition(position, item)
			category = 1
			if item.baseInfo.getItemPageType() == 4:
				category = 2
			db_package.putOneItemInPackage(characterId, this._packageType,
				item.baseInfo.getId(), \
				position, item.pack.getStack(), category)
		}
		putItemInPackDB(item, position, characterId) {
			//放置物品到数据库//
			item.InsertItemIntoDB(characterId = characterId)
			this.putItemByPosition(position, item)
			category = 1
			if item.baseInfo.getItemPageType() == 4:
				category = 2
			result = db_package.putOneItemInPackage(
				characterId, this._packageType, item.baseInfo.getId(), \
				position, item.pack.getStack(), category)
			return result
		}
		findSparePositionNum() {
			//获取剩余空隙位置的数量//
			return this._size - this._items.length
		}
		Arrange() {
				//包裹整理//
				this.MergePackage()# 先合并所有的物品
				itemsList = [item['itemComponent']
					for item in this._items
				]
				this._items = []
				itemsList.sort(cmp = null, key = lambda d: d.baseInfo
					.getItemTemplateId())
				for position in range(len(itemsList)) {
					itemRecord = {}
					itemRecord['position'] = position
					itemRecord['itemComponent'] = itemsList[
						position]
					db_package.moveItem(._packageType, itemsList[
						position], position)
					this._items.append(itemRecord)
				}
				countItemTemplateId(templateId) {
					//获取指定模板id的物品数量
					@
					param templateId: int 物品的模板id
						//
					count = 0
					for item in this._items:
						if item['itemComponent'].baseInfo.getItemTemplateId() ==
						templateId:
						count += item['itemComponent'].pack.getStack()
					return count
				}
				getItemTemplateIdPositions(templateId) {
					//指定模板id的物品数量及数量//
					infos = []
					for item in this._items:
						if item['itemComponent'].baseInfo.getItemTemplateId() ==
						templateId:
						info = {}
					info['position'] = item['position']
					info['stack'] = item['itemComponent'].pack
						.getStack()
					infos.append(info)
					return infos
				}
				getItemPositionById(itemId) {
					//根据物品的id获取物品的位置//
					for item in this._items:
						if item['itemComponent'].baseInfo.getId() ==
						itemId:
						return item['position']
					return -1
				}
				getItemByVirtualPostion(position, page) {
					//根据虚拟的坐标和包裹分页获取物品//
					postion = this.getRealPostion(
						position, page)
					item = this.getItemByPosition(postion)
					return item
				}
				getRealPostion(position, page) {
					//获取物品在包裹中的真实位置//
					realPostion = position + (page - 1) *
						30
					return realPostion
				}
				getVirtualPostion(postion) {
					//获取虚拟的坐标与分页//
					page = (postion / this.limit) + 1
					vpostion = postion % this.limit
					return page, vpostion
				}
